using MicroMsg.Manager;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.UserContrl
{
	public class SnsGroupAbstract : UserControl
	{
		private const string TAG = "SnsGroupAbstract";
		private const int cNMaxCnt = 5;
		public bool mBShowFriendOnly;
		public static DependencyProperty mSnsGroupProperty = DependencyProperty.Register("mSnsGroup", typeof(SnsTagInfo), typeof(SnsGroupAbstract), new PropertyMetadata(null, new PropertyChangedCallback(SnsGroupAbstract.OnmSnsGroupPropertyChanged)));
		public static DependencyProperty mGroupContactProperty = DependencyProperty.Register("mGroupContact", typeof(Contact), typeof(SnsGroupAbstract), new PropertyMetadata(null, new PropertyChangedCallback(SnsGroupAbstract.OnmGroupContactPropertyChanged)));
		private bool mBIsFirstLoaded = true;
		internal Button btnLayoutRoot;
		internal TextBlock txtGroupTitle;
		internal StackPanel panelHeads;
		private bool _contentLoaded;
		public event MMStandardUIEvent ItemClick;
		public SnsTagInfo mSnsGroup
		{
			get
			{
				return (SnsTagInfo)base.GetValue(SnsGroupAbstract.mSnsGroupProperty);
			}
			set
			{
				base.SetValue(SnsGroupAbstract.mSnsGroupProperty, value);
			}
		}
		public Contact mGroupContact
		{
			get
			{
				return (Contact)base.GetValue(SnsGroupAbstract.mGroupContactProperty);
			}
			set
			{
				base.SetValue(SnsGroupAbstract.mGroupContactProperty, value);
			}
		}
		public SnsGroupAbstract()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.Control_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.Control_Unloaded));
		}
		private static void OnmSnsGroupPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == SnsGroupAbstract.mSnsGroupProperty)
			{
				(dependencyObject_0 as SnsGroupAbstract).mSnsGroupPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void mSnsGroupPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				if (AddContactsPage.sAddContactsPageArgs != null)
				{
					this.mBShowFriendOnly = AddContactsPage.sAddContactsPageArgs.mBShowFriendOnlyInGroup;
				}
				this.RefreshItem();
			}
		}
		private static void OnmGroupContactPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == SnsGroupAbstract.mGroupContactProperty)
			{
				(dependencyObject_0 as SnsGroupAbstract).mGroupContactPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void mGroupContactPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_OldValue() != dependencyPropertyChangedEventArgs_0.get_NewValue())
			{
				this.refreshByCotnactGroup();
			}
		}
		private void Control_Loaded(object sender, RoutedEventArgs e)
		{
		}
		private void Control_Unloaded(object sender, RoutedEventArgs e)
		{
		}
		private void btnLayoutRoot_Click(object sender, object object_0)
		{
			if (this.ItemClick != null)
			{
				this.ItemClick(this, (this.mGroupContact == null) ? this.mSnsGroup : this.mGroupContact);
				return;
			}
			SnsGroupDetailPage.GotoThisPage(this.mSnsGroup, "");
		}
		private void refreshByCotnactGroup()
		{
			if (this.mGroupContact != null && !string.IsNullOrEmpty(this.mGroupContact.strUsrName))
			{
				this.txtGroupTitle.set_Text(ContactHelper.getDisplayName(this.mGroupContact.strUsrName, this.mGroupContact));
				List<string> membersByChatRoomName = ChatRoomMgr.getMembersByChatRoomName(this.mGroupContact.strUsrName);
				this.RefreshItemByList(membersByChatRoomName);
				return;
			}
			this.RefreshItemByList(null);
		}
		private void RefreshItem()
		{
			if (this.mSnsGroup == null)
			{
				this.RefreshItemByList(null);
				return;
			}
			this.txtGroupTitle.set_Text(this.mSnsGroup.strTagName);
			List<string> usersFromGroup = SnsGroupMgr.GetUsersFromGroup(this.mSnsGroup);
			this.RefreshItemByList(usersFromGroup);
		}
		private void RefreshItemByList(List<string> listUsers)
		{
			int num = 0;
			if (listUsers == null)
			{
				goto IL_F7;
			}
			using (List<string>.Enumerator enumerator = listUsers.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					Contact contact = StorageMgr.contact.method_0(current);
					if (contact != null && (!this.mBShowFriendOnly || (ContactMgr.isContact(contact) && !ContactMgr.isBlackList(contact))))
					{
						ContactHeadCtrl contactHeadCtrl = this.getCtrlInIndex(num);
						if (contactHeadCtrl == null)
						{
							contactHeadCtrl = ContactHeadCtrl.GetOneItem();
							contactHeadCtrl.set_Height(80.0);
							contactHeadCtrl.set_Width(80.0);
							contactHeadCtrl.set_Margin(new Thickness(0.0, 0.0, 10.0, 0.0));
							this.panelHeads.get_Children().Add(contactHeadCtrl);
						}
						contactHeadCtrl.mContact = contact;
						num++;
						if (num >= 5)
						{
							break;
						}
					}
				}
				goto IL_F7;
			}
			IL_E6:
			this.panelHeads.get_Children().RemoveAt(num);
			IL_F7:
			if (num >= this.panelHeads.get_Children().get_Count())
			{
				return;
			}
			goto IL_E6;
		}
		private ContactHeadCtrl getCtrlInIndex(int nIndex)
		{
			if (nIndex >= 0 && nIndex < this.panelHeads.get_Children().get_Count())
			{
				return this.panelHeads.get_Children().get_Item(nIndex) as ContactHeadCtrl;
			}
			return null;
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/Plugin/Sns/Common/UI/SnsGroup/SnsGroupAbstract.xaml", 2));
			this.btnLayoutRoot = (Button)base.FindName("btnLayoutRoot");
			this.txtGroupTitle = (TextBlock)base.FindName("txtGroupTitle");
			this.panelHeads = (StackPanel)base.FindName("panelHeads");
		}
	}
}
